
$(function browserType() {
    var userAgent = navigator.userAgent;
    var isOpera = userAgent.indexOf("Opera") > -1;
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;

    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7 || fIEVersion == 8 || fIEVersion == 9) {
            $("#uploadHigh").remove();
            $("#uploadHigh_rev").remove();
            return "IE";
        }
    }
    else {
        $("#uploadLow").remove();
        $("#uploadLow_rev").remove();
        return "0";
    }
    if (fIEVersion == 10) {
        $("#uploadLow").remove();
        $("#uploadLow_rev").remove();
        return "IE";
    }
});

//选中全选按钮，下面的checkbox全部选中
var selAll = document.getElementById("selAll");
function selectAll() {
    var obj = document.getElementsByName("checkAll");
    if (document.getElementById("selAll").checked == false) {
        //弹出菜单
        var divObj = document.getElementById("btn-group");
        divObj.style.display = "none";
        for (var i = 0; i < obj.length; i++) {
            obj[i].checked = false;
        }
    } else {
        //弹出菜单
        var divObj = document.getElementById("btn-group");
        divObj.style.display = "";
        for (var i = 0; i < obj.length; i++) {
            obj[i].checked = true;
        }
    }

}

//当选中所有的时候，全选按钮会勾上
function setSelectAll() {
    var obj = document.getElementsByName("checkAll");
    var count = obj.length;
    var selectCount = 0;
    for (var i = 0; i < count; i++) {
        if (obj[i].checked == true) {
            selectCount++;
        }
    }
    //弹出菜单
    if (selectCount > 0) {
        var divObj = document.getElementById("btn-group");
        divObj.style.display = "";
    } else if (selectCount == 0) {
        var divObj = document.getElementById("btn-group");
        divObj.style.display = "none";
    }
    if (count == selectCount) {
        document.all.selAll.checked = true;
    } else {
        document.all.selAll.checked = false;
    }
}

//反选按钮
function inverse() {
    var checkboxs = document.getElementsByName("checkAll");
    for (var i = 0; i < checkboxs.length; i++) {
        var e = checkboxs[i];
        e.checked = !e.checked;
        setSelectAll();
    }
}

function checkBoxNum_zopen(doSomething) {
    var arrays = new Array();
    var items = document.getElementsByName("test");
    for (var i = 0; i < items.length; i++) {
        if (items[i].checked) {
            arrays.push(items[i].value);
        }
    }
    str = arrays.join("-");
    document.getElementById(doSomething).value = str;
}
//提交已选checkBox
function checkBoxNum(doSomething) {
    var arrays = new Array();
    var items = document.getElementsByName("checkAll");
    for (var i = 0; i < items.length; i++) {
        if (items[i].checked) {
            arrays.push(items[i].value);
        }
    }
    str = arrays.join("-");
    document.getElementById(doSomething).value = str;
}

/*
var uploader = new plupload.Uploader({
    runtimes : 'html5,flash,silverlight,html4',
     
    browse_button : 'pickfiles', // you can pass in id...
    container: document.getElementById('container'), // ... or DOM Element itself
     
    url : "/examples/upload",
     
    filters : {
        max_file_size : '10mb',
        mime_types: [
            {title : "Image files", extensions : "jpg,gif,png"},
            {title : "Zip files", extensions : "zip"}
        ]
    },
 
    // Flash settings
    flash_swf_url : '/plupload/js/Moxie.swf',
 
    // Silverlight settings
    silverlight_xap_url : '/plupload/js/Moxie.xap',
     
 
    init: {
        PostInit: function() {
            document.getElementById('filelist').innerHTML = '';
 
            document.getElementById('uploadfiles').onclick = function() {
                uploader.start();
                return false;
            };
        },
 
        FilesAdded: function(up, files) {
            plupload.each(files, function(file) {
                document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
            });
        },
 
        UploadProgress: function(up, file) {
            document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
        },
 
        Error: function(up, err) {
            document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
        }
    }
});
 
uploader.init();
*/

function post(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        // alert(opt.name)
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}

function getUrlRelativePath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var relUrl = arrUrl[1].split("/")[1]
    return relUrl;
}

function do_copy() {
    var form = {
        'do_paste': 'do_copy',
        'currentIndex': document.getElementById("currentIndex").value,
        'uid': document.getElementById("uid_do_copy").value,
        'checkNum': document.getElementById("checkNum_do_copy").value,
    };
    post("/" + getUrlRelativePath() + "/" + "do_paste", form);
}

function do_move() {
    var form = {
        'do_paste': 'do_move',
        'currentIndex': document.getElementById("currentIndex").value,
        'uid': document.getElementById("uid_do_move").value,
        'checkNum': document.getElementById("checkNum_do_move").value,
    };
    post("/" + getUrlRelativePath() + "/" + "do_paste", form);
}

function do_mkdir() {
    var form = {
        'id': document.getElementById("currentIndex").value,
        'folderName': document.getElementById("folderName").value,
    };
    post("/" + getUrlRelativePath() + "/" + "do_mkdir", form);
}

function do_rename() {
    var form = {
        'id': document.getElementById('id_do_rename').value,
        'currentIndex': document.getElementById("currentIndex").value,
        'rename': document.getElementById("rename").value,
    };
    post("/" + getUrlRelativePath() + "/" + "do_rename", form);
}

function id_do_rename(obj) {
    document.getElementById('id_do_rename').value = obj.id;
}

function id_do_upload_rev(obj) {
    document.getElementById('id_do_upload_rev').value = obj.id;
}

$(".pagination ul").addClass("pagination");

function getTree() {
    var tree = eval('{{ tree|safe }}');
    return tree;
}

$('#tree_copy').treeview({data: getTree()})
$('#tree_copy').on('nodeSelected', function (event, data) {
    document.getElementById('uid_do_copy').value = data.uid;
})

$('#tree_move').treeview({data: getTree()})
$('#tree_move').on('nodeSelected', function (event, data) {
    document.getElementById('uid_do_move').value = data.uid;
})

function getNewTree() {
    $.post("/" + getUrlRelativePath() + "/" + "getNewTree", function (newTree) {
        var newTree = eval(newTree);
        $('#tree_copy').treeview({data: newTree});
        $('#tree_copy').on('nodeSelected', function (event, data) {
            document.getElementById('uid_do_copy').value = data.uid;
        })
        $('#tree_move').treeview({data: newTree});
        $('#tree_move').on('nodeSelected', function (event, data) {
            document.getElementById('uid_do_move').value = data.uid;
        })
    })
}

/*
function do_check_file(obj) {
    $.post("/" + getUrlRelativePath() + "/" + "do_check_file", {"uid": obj.id}, function (dataCheckFile) {
        document.getElementById('check_file_title').innerHTML = dataCheckFile['title'];
        document.getElementById('NoPermissioniframe').src = dataCheckFile['PreviewFileUrl'];
    })
}

function do_check_file_revision(obj) {
    document.getElementById('uid_do_check_file').value = obj.id
    $.post("/" + getUrlRelativePath() + "/" + "do_check_file_revision", {"uid": obj.id}, function (dataCheckFileRevision) {
        var tb = document.getElementById('check_file_revision_table');
        var rowNum = tb.rows.length;
        for (i = 1; i < rowNum; i++) {
            tb.deleteRow(i);
            rowNum = rowNum - 1;
            i = i - 1;
        }
        var rowNum = dataCheckFileRevision['revisionInfoList'].length;
        for (i = 0; i < rowNum; i++) {
            var tr = tb.insertRow(-1);
            var td = tr.insertCell(-1);
            td.innerHTML = "<a style='color:black;' id='" + dataCheckFileRevision['revisionInfoList'][i][1] + "' onclick='do_check_file(this);'>" + dataCheckFileRevision['revisionInfoList'][i][0] + " " + dataCheckFileRevision['revisionInfoList'][i][4] + "</a>";
        }
    })
}
*/

function id_do_check_file() {
    document.getElementById('id_do_rename').value = document.getElementById('uid_do_check_file').value;
    document.getElementById('id_do_upload_rev').value = document.getElementById('uid_do_check_file').value;
    document.getElementById("uid_do_copy").value = document.getElementById('uid_do_check_file').value;
    document.getElementById("checkNum_do_copy").value = [document.getElementById('uid_do_check_file').value];
    document.getElementById("uid_do_move").value = document.getElementById('uid_do_check_file').value;
    document.getElementById("checkNum_do_move").value = [document.getElementById('uid_do_check_file').value];
}

function do_check_file_download() {
    post("/" + getUrlRelativePath() + "/" + "download/" + document.getElementById('uid_do_check_file').value);
}

function do_check_file_download_EDOAssistent() {
    var form = {
        'checkNum_do_download': [document.getElementById('checkNum_do_download').value]
    };
    post("/" + getUrlRelativePath() + "/" + "downloadEDOAssistent", form);
}

function do_check_file_delete() {
    var form = {
        'checkNum_do_delete': [document.getElementById('uid_do_check_file').value]
    };
    post("/" + getUrlRelativePath() + "/" + "do_delete", form);
}

function do_screen() {
    var form = {
        'operation': document.getElementById("operation_span01").innerHTML,
        'mydatepicker': document.getElementById("mydatepicker").value,
        'currentTime': document.getElementById("operation_history_span01").innerHTML,
    };
    post("/" + getUrlRelativePath() + "/" + "filter_log", form);
}

